package day3;

import java.io.*;

//测试序列化和反序列化
public
class TestSerializable {
    public static
    void main(String[] args) throws IOException, ClassNotFoundException {
        //Output();
        Input();
    }
    //序列化
    public static void Output() throws IOException {
        FileOutputStream fos=new FileOutputStream("D:\\IO\\student.aaa");
        //序列化是转换成字节序列 文件内容不能乱改
        //所以本地文件会设置一个无法打开的后缀名
        ObjectOutputStream oos=new ObjectOutputStream(fos);
        Student s=new Student();
        StudentInfo info=new StudentInfo();
        s.setName("张三");
        s.setSex("男");
        s.setScore(99.9);
        info.setAddress("北京");
        info.setPhone("123456789");
        s.setInfo(info);
        oos.writeObject(s);
    }
    //反序列化
    public static void Input() throws IOException, ClassNotFoundException {
        FileInputStream fis=new FileInputStream("D:\\IO\\student.aaa");
        ObjectInputStream ois=new ObjectInputStream(fis);
        Student s=(Student)ois.readObject();
        System.out.println(s.getName());
        System.out.println(s.getSex());
        System.out.println(s.getScore());
        System.out.println(s.getInfo().getAddress());
        System.out.println(s.getInfo().getPhone());
    }
}
class Student implements Serializable {
    //序列化会默认生成一个序列号：用于检测序列化和反序列化的对象是否一致
    private static final long serialVersionUID = 11111111111l;
    private static String name;//static关键字修饰的属性不会被序列化
    private transient String sex;//transient关键字修饰的属性不会被序列化
    private double score;

    private StudentInfo info;

    public
    StudentInfo getInfo() {
        return info;
    }

    public
    void setInfo(StudentInfo info) {
        this.info = info;
    }

    public
    String getName() {
        return name;
    }

    public
    void setName(String name) {
        this.name = name;
    }

    public
    String getSex() {
        return sex;
    }

    public
    void setSex(String sex) {
        this.sex = sex;
    }

    public
    double getScore() {
        return score;
    }

    public
    void setScore(double score) {
        this.score = score;
    }
}
class StudentInfo implements Serializable {
    private Student student;
    private String address;
    private String phone;
    private String sex;

    public
    StudentInfo getInfo() {
        return info;
    }

    public
    void setInfo(StudentInfo info) {
        this.info = info;
    }

    private StudentInfo info;

    public
    String getAddress() {
        return address;
    }

    public
    void setAddress(String address) {
        this.address = address;
    }

    public
    String getPhone() {
        return phone;
    }

    public
    void setPhone(String phone) {
        this.phone = phone;
    }

    public
    String getSex() {
        return sex;
    }

    public
    void setSex(String sex) {
        this.sex = sex;
    }

    public
    Student getStudent() {
        return student;
    }
    public
    void setStudent(Student student) {
        this.student = student;
    }
}